package com.qifan.alb2b.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.qifan.alb2b.action.front.param.PersonNum;
import com.qifan.alb2b.dao.OrderInfoDao;
import com.qifan.alb2b.dao.TravelInfoDao;
import com.qifan.alb2b.dao.TravelItemInfoDao;
import com.qifan.alb2b.domain.ActivityInfoDomain;
import com.qifan.alb2b.domain.CompanyInfoDomain;
import com.qifan.alb2b.domain.OrderInfoDomain;
import com.qifan.alb2b.domain.RouteDetailInfoDomain;
import com.qifan.alb2b.domain.RouteInfoDomain;
import com.qifan.alb2b.domain.TravelInfoDomain;
import com.qifan.alb2b.domain.TravelItemInfoDomain;
import com.qifan.alb2b.domain.UserInfoDomain;
import com.qifan.alb2b.service.resource.IResource;
import com.qifan.alb2b.service.resource.ResourceFactory;
import com.qifan.alb2b.service.route.StPriceContext;
import com.qifan.alb2b.service.vo.ResourceVo;
import com.qifan.alb2b.service.vo.RouteDetailVo;
import com.qifan.alb2b.service.vo.RouteVo;
import com.qifan.alb2b.service.vo.UserVo;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DateUtil;
import com.qifan.alb2b.util.ID;

@Service
public class BookService {
	private StPriceContext stPriceContext;
	private OrderInfoDao orderInfoDao;
	private TravelInfoDao travelInfoDao;
	private TravelItemInfoDao travelItemInfoDao;
	private ResourceFactory resourceFactory;

	public void setStPriceContext(StPriceContext stPriceContext) {
		this.stPriceContext = stPriceContext;
	}
	
	public void setOrderInfoDao(OrderInfoDao orderInfoDao) {
		this.orderInfoDao = orderInfoDao;
	}

	public void setTravelInfoDao(TravelInfoDao travelInfoDao) {
		this.travelInfoDao = travelInfoDao;
	}

	public void setTravelItemInfoDao(TravelItemInfoDao travelItemInfoDao) {
		this.travelItemInfoDao = travelItemInfoDao;
	}

	public void setResourceFactory(ResourceFactory resourceFactory) {
		this.resourceFactory = resourceFactory;
	}

	/**
	 * 计算每种类型的总价格
	 * @param routeVo
	 * @param personNum
	 * @return
	 */
	public double[] calcTotalPrice(RouteVo routeVo,PersonNum personNum){
		return stPriceContext.calcTotalPrice(personNum, routeVo.getStPriceTable(), routeVo.getGroupType());
	}
	
	
	/**
	 * 预定，生成订单<br/>
	 * 1、保存订单对象orderInfo
	 * 2、保存行程详情travel
	 * 3、保存每个详情中旅游项目
	 */
	public void book(UserVo userVo,RouteVo routeVo,OrderInfoDomain orderInfo){
		RouteInfoDomain route=routeVo.getRoute();
		List<RouteDetailVo> detailList=routeVo.getDetailVoList();
		UserInfoDomain user=userVo.getUserInfo();
		CompanyInfoDomain company=userVo.getCompanyInfo();
		
		//=======保存订单对象=========
		orderInfo.setOrderId(ID.orderID());//订单号
		orderInfo.setCreateTime(new Date());//创建时间
		orderInfo.setUserId(user.getUserId());//用户id
		orderInfo.setCompanyId(company.getCompId());//公司id
		orderInfo.setRouteId(route.getRouteId());//关联原线路id
		orderInfo.setDay(route.getDay());//几天
		orderInfo.setNight(route.getNight());//几夜
		orderInfo.setGoCityCn(route.getOrgCityCn());//出发城市
		orderInfo.setGroupType(routeVo.getGroupType());//团队类型
		orderInfo.setOrderStatus(Constants.ORDER_STATUS_NOPAY);//设置订单状态
		orderInfo.setPayTime(DateUtil.next(new Date(),company.getPayTime()));//设置付款期限
		orderInfoDao.add(orderInfo);//保存订单
		
		//=======保存行程详情===========
		List<TravelInfoDomain> travelList=new ArrayList<TravelInfoDomain>();
		List<TravelItemInfoDomain> travelItemList=new ArrayList<TravelItemInfoDomain>();
		for(int i=0;i<detailList.size();i++){
			RouteDetailInfoDomain routeDetail=detailList.get(i).getRouteDetail();
			TravelInfoDomain travel=new TravelInfoDomain();
			travel.setTravelId(ID.next()+"");
			travel.setOrderId(orderInfo.getOrderId());
			travel.setWhichDay(i+1);
			travel.setDepart(routeDetail.getDepart());
			travel.setArrival(routeDetail.getArrivel());
			travel.setCityId(routeDetail.getCityId());
			travel.setCityCn(routeDetail.getCityCn());
			travel.setRemark(routeDetail.getRemark());
			travelList.add(travel);
			
			//=======保存详情中的旅游项目=====
			List<ResourceVo> resList=detailList.get(i).getResList();
			for(int j=0;j<resList.size();j++){
				ResourceVo rv=resList.get(j);
				TravelItemInfoDomain item=new TravelItemInfoDomain();
				item.setItemId(ID.next()+"");
				item.setTravelId(travel.getTravelId());
				item.setResourceType(rv.getResType());
				item.setResourceId(rv.getResId());
				item.setIsStd(rv.getIsStd());
				IResource resource = resourceFactory.newResouce(rv.getResType());
				resource.calcPrice(item, rv.getResource(), orderInfo.getTotalCount());
				travelItemList.add(item);
			}
		}
		travelInfoDao.add(travelList);//批量保存详情
		travelItemInfoDao.add(travelItemList);//批量保存资源
		
		
		
	}
}
